<?php

namespace app\server;

use \think\facade\Db;
use think\facade\Request;
use think\facade\Cache;
use app\server\XwechatServerServer;

class ExtraPathServer
{
    /**
     * 获取其他应用的链接
     * @param $id
     * @param $clickId
     * @param $event
     * @return void
     */
    public function getPath($params)
    {
        $table = 'app_wx_path';
        $path = '';
        $extraList = [
            'wechat'=>[
                ['type'=>'wechat','name'=>'xinglin','title'=>'杏林方剂学','dumpPath'=>'pages/index/index','banner_image'=>'http://cdn.yqdhz.com/jingyue/sucai/sucai2.png','native_image'=>'http://cdn.yqdhz.com/jingyue/sucai/sucai1.png'],
                ['type'=>'wechat','name'=>'ytjk','title'=>'易通驾考','dumpPath'=>'ytjk/index/index','banner_image'=>'http://cdn.yqdhz.com/jingyue/sucai/sucai2.png','native_image'=>'http://cdn.yqdhz.com/jingyue/sucai/sucai1.png'],
                ['type'=>'wechat','name'=>'jjbz','title'=>'精绝壁纸','dumpPath'=>'jjbz/index/index','banner_image'=>'http://cdn.yqdhz.com/jingyue/sucai/sucai2.png','native_image'=>'http://cdn.yqdhz.com/jingyue/sucai/sucai1.png'],
            ]
        ];
        if(!empty($params['uuid'])){
            $uuid = $params['uuid'];
            $ymd = date('Ymd',time());
            $rand = 0;
            $list = $extraList['wechat'];
            $listLength = count($list);
            if($listLength > 0){
                $rawRand = mt_rand(0,$listLength -1);
                $row = $list[$rawRand];
                $rowPath = find($table,'path',['uuid'=>$uuid,'active_ymd'=>$ymd,'active_status'=>1]);
                if(!empty($rowPath)){
                    $path =  $rowPath['path'];
                }else{
                    $rowPath = find($table,'id,path',['wx_name'=>$row['name'],'active_ymd'=>$ymd,'active_status'=>0]);
                    if(!empty($rowPath)){
                        $path =  $rowPath['path'];
                        edit($table,['id'=>$rowPath['id']],['active_status'=>1,'wx_name'=>$row['name'],'uuid'=>$uuid,'active_ymd'=>$ymd]);
                    }else{
                        try{
                            $path = app(XwechatServerServer::class)->getWxPath($row['name'],$row['dumpPath']);
                            if(!empty($path)){
                                $addData = [
                                    'path'=>$path,
                                    'uuid'=>$uuid,
                                    'active_ymd'=>$ymd,
                                    'wx_name'=>$row['name']
                                ];
                                add($table,$addData);
                            }
                        }catch(\Exception $e){
                        }
                    }
                }
            }
        }
        if(!empty($path)){
            $row['path'] = $path;
            $row['banner_image'] = 'http://cdn.yqdhz.com/jingyue/sucai/banner_'.mt_rand(1,3).'.png';
            $row['native_image'] = 'http://cdn.yqdhz.com/jingyue/sucai/native_'.mt_rand(1,3).'.png';
            return $row;
        }else{
            return [];
        }
    }



}


?>
